package com.aiju.cash.ui.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.main.entity.SelectEmployy;

import java.util.List;

/**
 * 作者：流冥 on 2017/6/15 16:52
 * 说明：
 */

public class SelectEmployAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<SelectEmployy> mList;

    public SelectEmployAdapter(Context context, List<SelectEmployy> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_select_employee, parent, false);

        return new ContentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        showView((ContentViewHolder) holder, mList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    private class ContentViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        ImageView check;
        TextView title;

        public ContentViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            check = (ImageView) itemView.findViewById(R.id.check);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }

    void showView(ContentViewHolder holder, SelectEmployy selectShop, final int position) {
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearAllSelectedShop();
                mList.get(position).setSelected(true);
                mListener.setOnItemClickListenering(mList.get(position).getOperatorId());
                notifyDataSetChanged();

            }
        });
        showTextAndCheckImageView(holder, selectShop.isSelected());
        holder.title.setText(selectShop.getUserName());

    }

    void showTextAndCheckImageView(ContentViewHolder holder, boolean isSelect) {
        if (isSelect) {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.color_00A0E8));
            holder.check.setVisibility(View.VISIBLE);
        } else {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.check.setVisibility(View.GONE);
        }

    }

    /**
     * 筛选出被选择的店铺，若没有被选择的，返回null
     */
    public SelectEmployy getSelectedShop() {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).isSelected()) {
                return mList.get(i);
            }
        }
        return null;
    }

    void clearAllSelectedShop() {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setSelected(false);
        }
    }

    public void setList(List<SelectEmployy>list){
        this.mList = list;
        notifyDataSetChanged();
    }



    public interface  OnItemClickListener{
        void setOnItemClickListenering(String storeId);
    }

    private OnItemClickListener mListener;
    public void setOnItenClickListner(OnItemClickListener listner){
        mListener = listner;
    }

}
